{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8f8f16bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d53c7385",
   "metadata": {},
   "source": [
    "# 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0650cff4",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.array([[1,2,3,4],[5,6,7,8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "19593ea2",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.arange(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "823ef8ed",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  3,  5,  7],\n",
       "       [ 5,  7,  9, 11]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data + arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bde95e1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.arange(2).reshape(2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "21a84a63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 4],\n",
       "       [6, 7, 8, 9]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data + arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2a91dbd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 4, 5],\n",
       "       [6, 7, 8, 9]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4464a02f",
   "metadata": {},
   "outputs": [],
   "source": [
    "data[:,1:] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "24de15cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 0],\n",
       "       [5, 0, 0, 0]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d5c0ca6",
   "metadata": {},
   "source": [
    "# 布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "75155b50",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.random.randn(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fe2d39a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.79570753,  0.73725249,  1.85648592],\n",
       "       [-2.0005016 ,  1.4431807 ,  0.51694609],\n",
       "       [-0.3926624 ,  0.20147135,  0.40380166]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "de61459c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False,  True,  True],\n",
       "       [False,  True,  True],\n",
       "       [False,  True,  True]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "274e3976",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.73725249, 1.85648592, 1.4431807 , 0.51694609, 0.20147135,\n",
       "       0.40380166])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[arr > 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0afe42ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr2 = np.array([\"red\",\"black\",\"red\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "061421b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False,  True])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 == \"red\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9312d515",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.79570753,  0.73725249,  1.85648592],\n",
       "       [-0.3926624 ,  0.20147135,  0.40380166]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[arr2 == \"red\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5efcbef3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.73725249, 1.85648592],\n",
       "       [0.20147135, 0.40380166]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[arr2 == \"red\",1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b2312a3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr3 = arr.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "79bf21a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr3[arr < 0] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "23debff0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.        , 0.73725249, 1.85648592],\n",
       "       [0.        , 1.4431807 , 0.51694609],\n",
       "       [0.        , 0.20147135, 0.40380166]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18b33a9e",
   "metadata": {},
   "source": [
    "# 花式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a22fe8be",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr4 = np.arange(24).reshape(6,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f63460e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15],\n",
       "       [16, 17, 18, 19],\n",
       "       [20, 21, 22, 23]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1c675e87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  5,  6,  7],\n",
       "       [12, 13, 14, 15],\n",
       "       [20, 21, 22, 23]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4[[1,3,5]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f004738a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 21, 22, 23],\n",
       "       [16, 17, 18, 19],\n",
       "       [ 0,  1,  2,  3]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4[[-1,-2,0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0fdfb33e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  3, 20, 23])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4[[0,0,5,5],[0,3,0,3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "f63ee1c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  3],\n",
       "       [20, 23]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4[np.ix_([0,5],[0,3])]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
